﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VNWIKI_WEB.DAO;
using VNWIKI_WEB.Models;
using System.IO;

namespace VNWIKI_WEB.Controllers
{
    public class UploadController : Controller
    {
      
        // GET: /Upload/

        public ActionResult Index()
        {
            User user = (User)Session["user"];
            if (user != null)
            {
                HomeDAO home = new HomeDAO();
                List<Category> catList = home.GetCatList();
                DataList dtl = new DataList();
                dtl.CatList = catList;
                return View("AlbumCreate", dtl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        
        public int ReceiveFileUpload(HttpPostedFileBase MyFile, FormCollection form)
        {
             form.Get("sessionId");

            int myResult;
            if (MyFile == null)
            {
                myResult = 0;
            }
            try
            {
           
                var fileName = Path.GetFileName(MyFile.FileName);
                var folderPath = GetUrlToUpLoadImage();
                var path = Path.Combine(folderPath, fileName);
                MyFile.SaveAs(path);
                string urlImage = Session["urlImage"].ToString() + fileName;
                InsertPhoto(urlImage, fileName);
                myResult = 1;
            }
            catch { myResult = 0; }
            return myResult;
        }

        public void InsertPhoto(string link, string photoName)
        {
            User user = (User)Session["user"];
            HomeDAO home = new HomeDAO();
            AdminDAO admin = new AdminDAO();
            Album album = home.GetAlbumNewInsert(user.ID);
            admin.InsertPhoto(album, photoName, link);
        }

        public string GetUrlToUpLoadImage()
        {
            HomeDAO home = new HomeDAO();
            string albumName = (home.GetAlbumList().Count +1).ToString();
            string url = "";

            string userName = Session["userName"].ToString();
            
            string urlTemp = "UserUpload" + "\\" + userName + "\\" + albumName;
            Session["urlImage"] = "UserUpload" + "/" + userName + "/" + albumName + "/";
            url = Path.Combine(Server.MapPath("~/images"), urlTemp);
            if (!Directory.Exists(url))
                Directory.CreateDirectory(url);
            return url;
        }

        public ActionResult InsertAlbum(string albumName, int catID, string tags)
        {
            User user = (User)Session["user"];
            if (user != null)
            {

                if (!string.IsNullOrEmpty(albumName.Trim()) && catID != 0)
                {

                    HomeDAO home = new HomeDAO();
                    List<Category> catList = home.GetCatList();
                    DataList dtl = new DataList();
                    dtl.CatList = catList;
                    AdminDAO admin = new AdminDAO();
                    int rs;
                    rs = admin.InsertAlbum(albumName, tags, catID, user.ID);
                    if (rs.Equals(1))
                    {
                        ViewData["albumName"] = albumName;
                        //ViewData["sessionId"] = System.Web.HttpContext.Current.Session.SessionID;
                        string u = Session.SessionID;
                        return View("PhotoUpload", dtl);
                    }
                    else
                    {
                        return View("AlbumCreate", dtl);
                    }
                }
                else
                {
                    ViewData["message"] = "Không được để trống mục Tên Album và Thể loại.";
                    return Redirect(Request.UrlReferrer.AbsoluteUri);
                }
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }

        }

    }
}
